package codetop.microsoft.T43;

/**
 * @Author: 18362
 * @Create: 2022-09-08 3:32:34 星期四
 */
class Solution {
    public String multiply(String num1, String num2) {
        int[] ans = new int[num1.length() + num2.length()];
        int offset = 0;
        for (int i=num2.length()-1; i >= 0; --i) {
            int carry = 0, a = num2.charAt(i) - '0';
            for (int j=num1.length()-1; j >= 0; --j) {
                int b = num1.charAt(j) - '0';
                int res = a * b + carry + ans[offset + num1.length()-1-j];
                ans[offset + num1.length()-1-j] = res % 10;
                carry = res / 10;
            }
            int j = offset + num1.length();
            while (carry != 0) {
                int res = ans[j] + carry;
                ans[j] = res % 10;
                carry = res / 10;
            }
            offset++;
        }
        StringBuilder sb = new StringBuilder();
        for (int i=ans.length-1; i>=0; --i) {
            if (sb.length() != 0 || ans[i] > 0)
                sb.append(ans[i]);
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }
}